Program execution method, program processing method, and related device

ABSTRACT

This application discloses a program execution method, which may be applied to the field of program processing. The method includes: receiving a call request of a first program for a small stub function; loading the small stub function based on the call request; and determining, based on the small stub function, whether to load a complementary real function corresponding to the small stub function. A function that may actually need to be called by the first program is the complementary real function, and the small stub function may be understood as a buffer for loading the complementary real function. Whether to load the complementary real function can be controlled through addition of the buffer, so that a requirement of the first program for a running memory can be reduced.

CROSS-REFERENCE TO RELATED APPLICATIONS

This application is a continuation of International Application No.PCT/CN2021/101965, filed on Jun. 24, 2021, which claims priority toChinese Patent Application No. 202011198663.0, filed on Oct. 31, 2020.The disclosures of the aforementioned applications are herebyincorporated by reference in their entireties.

TECHNICAL FIELD

This application relates to the field of program processing, and inparticular, to a program execution method, a program processing method,and a related device.

BACKGROUND

A technology of “a miniaturized operating system (OS)-complementary OS”is used in the industry, to simplify occupation of a device memory of asoftware system in a computer system. The miniaturized OS is alsoreferred to as a small image, and the complementary OS is also referredto as a complementary image.

The small image and complementary image technology is a process in whicha computer device generates the small image and the complementary imagebased on source code and a process in which the small image and thecomplementary image are used. A particular generation manner is asfollows. First, the computer device distinguishes between a function setB in the source code that has a dependency with an entry point of aprogram, and a function set A in the source code that has no dependencywith the entry point. The computer device deletes a function in thefunction set A from the source code, and retains a function in thefunction set B to obtain the small image. The computer device retainsthe function in the function set A from the source code, and replacesthe function in the function set B with a null function to obtain thecomplementary image. The function in the function set A in thecomplementary image is referred to as a real function. The null functionset in the complementary image corresponds to the function set B in thesmall image, or functions in the null function set in the complementaryimage one-to-one correspond to functions in the function set B in thesmall image. The small image is a static link, and the complementaryimage is a dynamic link. When code in a third-party program includes afunction in the complementary image and when the third-party program isstarted, a first device loads the function to a running memory inadvance, and waits for the function to be executed.

The running memory of the first device is usually limited. If the codein the third-party program calls excessive functions, the running memorymay be insufficient; consequently, the third-party program fails to run.

SUMMARY

This application provides a program execution method, a programprocessing method, and related devices to reduce a requirement of afirst program for a running memory.

A first aspect of this application provides a program execution method,applied to a computer system, where the computer system includes a firstsmall image. The method may be performed by the computer system, or maybe performed by a first device on which the computer system isinstalled. The following uses an example in which the method may beperformed by the first device for description.

The method includes: The first device receives a call request of a firstprogram for a small stub function, where the small stub function is afunction in a small stub function set, and the first small imageincludes the small stub function set and a small real function set;loads the small stub function based on the call request; and determines,based on the small stub function, whether to load a complementary realfunction corresponding to the small stub function, where thecomplementary real function is a function in a complementary realfunction set, a first complementary image includes the complementaryreal function set and a complementary stub function set, thecomplementary stub function set corresponds to the small real functionset, and the complementary real function set corresponds to the smallstub function set.

A function that may actually need to be called by the first program isthe complementary real function, and the small stub function may beunderstood as a buffer for loading the complementary real function.Whether to load the complementary real function can be controlledthrough addition of the buffer, so that a requirement of the firstprogram for a running memory can be reduced. For example, when a relatedfunction corresponding to the complementary real function is not enabledin the first program, the complementary real function is not loaded.

According to the first aspect of this application, in a firstimplementation of the first aspect of this application, the first deviceattempts to run the small stub function according to an instruction ofthe first program, and if detecting an exception, loads thecomplementary real function. When the first program needs to enable therelated function corresponding to the complementary real function, thefirst device may receive the instruction of the first program. It shouldbe noted that the instruction may be a change of a judgment variablethat affects a program process in code (or a function called by thecode) of the first program, and the change of the judgment variabletriggers running of the small stub function. After triggering the smallstub function, the first device attempts to run the small stub function.The attempt means that the first device is not necessarily capable ofrunning the small stub function, and may only have an intent ofattempting to run the small stub function. For example, the first deviceobtains an address of the small stub function, and is partially ready torun the small stub function. By detecting the exception, the firstdevice may know that the first program is to start the related functioncorresponding to the complementary real function, and further load thecomplementary real function. Conversely, before detecting the exception,the first device may not need to load the complementary real function.In brief, in this application, loading of the complementary realfunction is triggered by detecting the related exception of the smallstub function. When the exception is detected, it indicates that runningof the small stub function is not completed successfully. In this case,the first program does not continue to be executed because execution ofthe small stub function is completed. Therefore, in this application,not only the buffer is added for loading the complementary realfunction, but also the first program is suspended. This provides a timebasis for loading the complementary real function, reduces errors thatmay occur in the first program, and improves user experience.

According to the first aspect of this application or the firstimplementation of the first aspect, in a second implementation of thefirst aspect of this application, the small real function set is a setof functions that are run by using target code in a process of running atarget case set, and the complementary real function set is a set offunctions that are not run by using the target code in the process ofrunning the target case set. A small image and a complementary imagethat are obtained based on the small real function set and thecomplementary real function set are referred to as a small image and acomplementary image that are obtained based on a coverage rate. A smallimage and a complementary image that are obtained based on the functionset B and the function set A in the background are referred to as asmall image and a complementary image that are obtained based on adependency rate. In some scenarios, a small real function set in thesmall image obtained based on the coverage rate is less than thefunction set B in the small image obtained based on the dependency rate.A smaller range of the small real function set indicates a smallerquantity of functions directly loaded by the first program. This furtherreduces the requirement of the first program for the running memory.Direct loading refers to loading without using the small stub function.For ease of description, a function loaded by using the small stubfunction is referred to as an indirectly loaded function. The indirectlyloaded function is not directly loaded to the running memory of thefirst device, but the first device loads the function to the runningmemory only when the first program enables a function related to thefunction. If not starting the function related to the function, thefirst device may not load the function. In brief, a range of functionsthat are directly loaded is narrowed, and a range of functions that areindirectly loaded is widened.

According to the second implementation of the first aspect of thisapplication, in a third implementation of the first aspect of thisapplication, the method further includes: The first device loads atarget function. The target function includes a target character string,a data volume of the target character string is less than a data volumeof a source character string, the target code includes the targetfunction, the target code is obtained based on source code, the sourcecode includes a source function, and the source function includes thesource character string. The source function corresponds to the targetfunction. The correspondence herein means that the source function andthe target function have a same capability of processing characterstrings, but process two different character strings: the targetcharacter string and the source character string. In a plurality ofscenarios, a user does not need to read, by using the first device,source character strings such as log data of a washing machine or analarm log of UE. When functions corresponding to the source characterstrings are loaded, the source character strings are also loaded to therunning memory, and consequently occupy the running memory of the firstdevice. In this application, the source character string is processed byusing an “ID-based” method. In this way, the requirement for the runningmemory of the first device can be reduced, security of the sourcecharacter string is improved, and the source character string is noteasily read by a malicious attacker.

According to the third implementation of the first aspect of thisapplication, in a fourth implementation of the first aspect of thisapplication, after the first device loads the target function, themethod further includes: The first device sends the target characterstring to a second device, where the target character string is used bythe second device to obtain the source character string based on atarget correspondence and the target character string. Restoring thesource character string by using the target character string canfacilitate reading and improve user experience.

It should be noted that, for ease of description, the second deviceappears in a plurality of implementations of the first aspect of thisapplication. The second device is one or more second devices differentfrom the first device. In other words, second devices in differentimplementations may be a same computer device, or may be differentcomputer devices.

According to any one of the first implementation to the fourthimplementation of the first aspect of this application, in a fifthimplementation of the first aspect of this application, the first deviceloads the complementary real function from the second device based onthe small stub function. The first complementary image is stored in thesecond device, so that a requirement for storage space of the firstdevice can be reduced. When the second implementation is referenced inthe fifth implementation, compared with the small image obtained basedon the dependency rate, the small image obtained based on the coveragerate usually has a smaller size or a smaller data volume. Therefore, therequirement for the storage space of the first device can be furtherreduced. In addition, the coverage rate is obtained based on the targetcase set. The target case set is properly set, so that a functioncommonly used by the first device may be in the first small image, and afunction not commonly used may be in the first complementary image.Therefore, a proper compromise can be made between reducing a datatransmission amount and reducing the requirement for the memory of thefirst device. The data transmission amount is a data volume of thecomplementary real function sent by the second device to the firstdevice. Therefore, a combination of a distributed scenario, and thesmall image and the complementary image that are obtained based on thecoverage rate has a greater advantage than a combination of the smallimage and the complementary image that are obtained based on thedependency rate.

According to any one of the first aspect of this application, or thefirst implementation to the fourth implementation of the first aspect,in a sixth implementation of the first aspect of this application, thefirst complementary image and the first small image are stored indifferent storage media of the first device. When the sixthimplementation is referenced in the second implementation, it can belearned from the foregoing description of the fifth implementation thatthe first small image includes the function commonly used by the firstdevice, and the first complementary image includes the function notcommonly used by the first device. Therefore, a requirement of the firstdevice for the first small image is different from a requirement for thefirst complementary image, for example, a read frequency or a readspeed. Therefore, storing the first complementary image and the firstsmall image in different storage media, for example, storage media withdifferent costs, can reduce costs of the first device.

According to any one of the first aspect of this application, or thefirst implementation to the sixth implementation of the first aspect, ina seventh implementation of the first aspect of this application, ifdetermining, based on the small stub function, to load the complementaryreal function, the first device sends related information of thecomplementary real function to the second device, where the relatedinformation is used by the second device to obtain a second small imagebased on the related information, and the second small image includesthe small real function set and the complementary real function. Thecomplementary real function is added to the second small image, so thata speed of calling the complementary real function by the first programcan be improved, and user experience can be improved.

A second aspect of this application provides a program processingmethod.

The method includes: A second device sends a first small image to afirst device. The first small image includes a small real function setand a small stub function set, the small stub function set correspondsto a complementary real function set of a first complementary image, thesmall real function set corresponds to a complementary stub function setof the first complementary image, the small stub function set includes asmall stub function, the small stub function is used by a first deviceto determine, based on the small stub function, whether to load acomplementary real function corresponding to the small stub function,and the complementary real function set includes the complementary realfunction.

For description of beneficial effects of the second aspect of thisapplication, refer to the foregoing description of beneficial effects ofthe first aspect of this application.

According to the second aspect of this application, in a firstimplementation of the second aspect of this application, the small stubfunction is used by the first device to attempt to run the small stubfunction according to an instruction of a first program, and if anexception is detected, the complementary real function is loaded.

According to the second aspect of this application or the firstimplementation of the second aspect, in a second implementation of thesecond aspect of this application, the small real function set is a setof functions that are run by using target code in a process of running atarget case set, and the complementary real function set is a set offunctions that are not run by using the target code in the process ofrunning the target case set.

According to the second implementation of the second aspect of thisapplication, in a third implementation of the second aspect of thisapplication, the target code includes a target function, the targetfunction includes a target character string, a data volume of the targetcharacter string is less than a data volume of a source characterstring, the target code is obtained based on source code, the sourcecode includes a source function, and the source function includes thesource character string.

According to the third implementation of the second aspect of thisapplication, in a fourth implementation of the second aspect of thisapplication, the target character string is used with a targetcorrespondence to obtain the source character string.

According to any one of the second aspect of this application, or thefirst implementation to the fourth implementation of the second aspect,in a fifth implementation of the second aspect of this application, thefirst device stores the first small image, and the first small image andthe first complementary image are stored in different devices.

According to any one of the second aspect of this application, or thefirst implementation to the fourth implementation of the second aspect,in a sixth implementation of the second aspect of this application, thefirst complementary image and the first small image are stored indifferent storage media of the first device.

According to any one of the second aspect of this application, or thefirst implementation to the sixth implementation of the second aspect,in a seventh implementation of the second aspect of this application,the second device receives related information that is of thecomplementary real function and that is sent by the first device; andthe second device obtains a second small image based on the relatedinformation, where the second small image includes the small realfunction set and the complementary real function.

A third aspect of this application provides a program executionapparatus. The apparatus is installed with a computer system, thecomputer system includes a first small image, and the apparatusincludes: a receiving module, configured to receive a call request of afirst program for a small stub function, where the small stub functionis a function in a small stub function set, and the first small imageincludes the small stub function set and a small real function set; aloading module, configured to load the small stub function based on thecall request; and a processing module, configured to determine, based onthe small stub function, whether to load a complementary real functioncorresponding to the small stub function, where the complementary realfunction is a function in a complementary real function set, a firstcomplementary image includes the complementary real function set and acomplementary stub function set, the complementary stub function setcorresponds to the small real function set, and the complementary realfunction set corresponds to the small stub function set.

According to the third aspect of this application, in a firstimplementation of the third aspect of this application, the processingmodule is configured to: attempt to run the small stub functionaccording to an instruction of a first program, and if detecting anexception, load the complementary real function.

According to the third aspect of this application or the firstimplementation of the third aspect, in a second implementation of thethird aspect of this application, the small real function set is a setof functions that are run by using target code in a process of running atarget case set, and the complementary real function set is a set offunctions that are not run by using the target code in the process ofrunning the target case set.

According to the second implementation of the third aspect of thisapplication, in a third implementation of the third aspect of thisapplication, the loading module is further configured to load a targetfunction. The target function includes a target character string, a datavolume of the target character string is less than a data volume of asource character string, the target code includes the target function,the target code is obtained based on source code, the source codeincludes a source function, and the source function includes the sourcecharacter string.

According to the third implementation of the third aspect of thisapplication, in a fourth implementation of the third aspect of thisapplication, the apparatus further includes: a first sending module,configured to send the target character string to a second device, wherethe target character string is used by the second device to obtain thesource character string based on a target correspondence and the targetcharacter string.

According to any one of the first implementation to the fourthimplementation of the third aspect of this application, in a fifthimplementation of the third aspect of this application, the processingmodule is configured to load the complementary real function from thesecond device based on the small stub function.

According to any one of the third aspect of this application, or thefirst implementation to the fourth implementation of the third aspect,in a sixth implementation of the third aspect of this application, thefirst complementary image and the first small image are stored indifferent storage media of the first device.

According to any one of the third aspect of this application, or thefirst implementation to the sixth implementation of the third aspect, ina seventh implementation of the third aspect of this application, theapparatus further includes: a second sending module, configured to: ifdetermining, based on the small stub function, to load the complementaryreal function, send related information of the complementary realfunction to the second device, where the related information is used bythe second device to obtain a second small image based on the relatedinformation, and the second small image includes the small real functionset and the complementary real function.

A fourth aspect of this application provides a computer system, wherethe system includes a processor and a memory.

The memory stores a first small image.

The processor is configured to receive a call request of a first programfor a small stub function, where the small stub function is a functionin a small stub function set, and the first small image includes thesmall stub function set and a small real function set.

The processor is further configured to load the small stub functionbased on the call request.

The processor is further configured to determine, based on the smallstub function, whether to load a complementary real functioncorresponding to the small stub function. The complementary realfunction is a function in a complementary real function set, a firstcomplementary image includes the complementary real function set and acomplementary stub function set, the complementary stub function setcorresponds to the small real function set, and the complementary realfunction set corresponds to the small stub function set.

According to the fourth aspect of this application, in a firstimplementation of the fourth aspect of this application, the processoris configured to: attempt to run the small stub function according to aninstruction of a first program, and if detecting an exception, load thecomplementary real function.

According to the fourth aspect of this application or the firstimplementation of the fourth aspect, in a second implementation of thefourth aspect of this application, the small real function set is a setof functions that are run by using target code in a process of running atarget case set, and the complementary real function set is a set offunctions that are not run by using the target code in the process ofrunning the target case set.

According to the second implementation of the fourth aspect of thisapplication, in a third implementation of the fourth aspect of thisapplication, the processor is further configured to load a targetfunction. The target function includes a target character string, a datavolume of the target character string is less than a data volume of asource character string, the target code includes the target function,the target code is obtained based on source code, the source codeincludes a source function, and the source function includes the sourcecharacter string.

According to the third implementation of the fourth aspect of thisapplication, in a fourth implementation of the fourth aspect of thisapplication, the processor is further configured to send the targetcharacter string to a second device, where the target character stringis used by the second device to obtain the source character string basedon a target correspondence and the target character string.

According to any one of the first implementation to the fourthimplementation of the fourth aspect of this application, in a fifthimplementation of the fourth aspect of this application, the processoris configured to load the complementary real function from the seconddevice based on the small stub function.

According to any one of the fourth aspect of this application, or thefirst implementation to the fourth implementation of the fourth aspect,in a sixth implementation of the fourth aspect of this application, thefirst complementary image and the first small image are stored indifferent storage media of the first device.

According to any one of the fourth aspect of this application, or thefirst implementation to the sixth implementation of the fourth aspect,in a seventh implementation of the fourth aspect of this application,the processor is further configured to: if determining, based on thesmall stub function, to load the complementary real function, sendrelated information of the complementary real function to the seconddevice, where the related information is used by the second device toobtain a second small image based on the related information, and thesecond small image includes the small real function set and thecomplementary real function.

A fifth aspect of this application provides a program processingapparatus. The apparatus includes a sending module, configured to send afirst small image to a first device. The first small image includes asmall real function set and a small stub function set, the small stubfunction set corresponds to a complementary real function set of a firstcomplementary image, the small real function set corresponds to acomplementary stub function set of the first complementary image, thesmall stub function set includes a small stub function, the small stubfunction is used by a first device to determine, based on the small stubfunction, whether to load a complementary real function corresponding tothe small stub function, and the complementary real function setincludes the complementary real function.

According to the fifth aspect of this application, in a firstimplementation of the fifth aspect of this application, the small stubfunction is used by the first device to attempt to run the small stubfunction according to an instruction of a first program, and if anexception is detected, the complementary real function is loaded.

According to the fifth aspect of this application or the firstimplementation of the fifth aspect, in a second implementation of thefifth aspect of this application, the small real function set is a setof functions that are run by using target code in a process of running atarget case set, and the complementary real function set is a set offunctions that are not run by using the target code in the process ofrunning the target case set.

According to the second implementation of the fifth aspect of thisapplication, in a third implementation of the fifth aspect of thisapplication, the target code includes a target function, the targetfunction includes a target character string, a data volume of the targetcharacter string is less than a data volume of a source characterstring, the target code is obtained based on source code, the sourcecode includes a source function, and the source function includes thesource character string.

According to the third implementation of the fifth aspect of thisapplication, in a fourth implementation of the fifth aspect of thisapplication, the target character string is used with a targetcorrespondence to obtain the source character string.

According to any one of the fifth aspect of this application, or thefirst implementation to the fourth implementation of the fifth aspect,in a fifth implementation of the fifth aspect of this application, thefirst device stores the first small image, and the first small image andthe first complementary image are stored in different devices. Thedevice storing the first complementary image is configured to providethe complementary real function set for a plurality of devices, and theplurality of devices include the first device.

According to any one of the fifth aspect of this application, or thefirst implementation to the fourth implementation of the fifth aspect,in a sixth implementation of the fifth aspect of this application, thefirst complementary image and the first small image are stored indifferent storage media of the first device.

According to any one of the fifth aspect of this application, or thefirst implementation to the sixth implementation of the fifth aspect, ina seventh implementation of the fifth aspect of this application, theapparatus further includes: a receiving module, configured to receiverelated information that is of the complementary real function and thatis sent by the first device; and a processing module, configured toobtain a second small image based on the related information, where thesecond small image includes the small real function set and thecomplementary real function.

A sixth aspect of this application provides a program execution device,including a processor and a memory, where the memory stores a firstsmall image and an executable program, and the first small imageincludes a small stub function. The processor is configured to: obtainthe executable program from the memory, and process the small stubfunction based on the executable program. A processing method includesthe method according to any one of the first aspect or theimplementations of the first aspect.

A seventh aspect of this application provides a program processingdevice, including a processor and a memory, where the memory stores aprocessing program. The processor is configured to: obtain theprocessing program from the memory, and obtain a first small image basedon the processing program and source code. A processing method includesthe method according to any one of the second aspect or theimplementations of the second aspect.

An eighth aspect of this application provides a computer storage medium,where the computer storage medium stores instructions. When theinstructions are executed on a computer, the computer is enabled toperform the method according to any one of the first aspect or theimplementations of the first aspect; or the computer is enabled toperform the method according to any one of the second aspect or theimplementations of the second aspect.

A ninth aspect of this application provides a computer program product.When the computer program product is executed on a computer, thecomputer is enabled to perform the method according to any one of thefirst aspect or the implementations of the first aspect; or the computeris enabled to perform the method according to any one of the secondaspect or the implementations of the second aspect.

BRIEF DESCRIPTION OF DRAWINGS

FIG. 1 is a flowchart of tailoring a function based on a functiondependency rate;

FIG. 2 is a flowchart of obtaining a small image and a complementaryimage based on source code;

FIG. 3 is a flowchart of a processing program according to an embodimentof this application;

FIG. 4 is a diagram of an application scenario according to anembodiment of this application;

FIG. 5 is a flowchart of a program processing method according to anembodiment of this application;

FIG. 6 is a flowchart of miniaturizing a character string by using an“ID-based” method according to an embodiment of this application;

FIG. 7 is a diagram of a relationship between a function in source codehaving a dependency and an overwritten function in the source code;

FIG. 8 is a flowchart of obtaining an image based on a coverage rateaccording to an embodiment of this application;

FIG. 9 is a flowchart of a program execution method according to anembodiment of this application;

FIG. 10 is a diagram of a program execution apparatus according to anembodiment of this application;

FIG. 11 is a diagram of a program processing apparatus according to anembodiment of this application; and

FIG. 12 is a diagram of a first device or a second device according toan embodiment of this application.

DESCRIPTION OF EMBODIMENTS

Embodiments of this application are applied to the field of programprocessing, and provide a program execution method, a program processingmethod, and a related device, to reduce a requirement of a first programfor a running memory.

For ease of understanding the technical solutions provided in thisapplication, the following first describes some related concepts.

A functional dependency is a mathematically derived term that representsa dependency of a value of one attribute or set of attributes on a valueof another attribute or set of attributes. It is assumed that R(U) is arelational pattern on an attribute set U, and X and Y are subsets of U.For any two possible relationships r1 and r2 of R(U), if r1[x]=r2[x],r1[y]=r2[y]; or if r1[y] is not equal to r2[y], r1 [x] is not equal tor2[x]. X determines Y, or Y depends on X.

A function dependency rate (dependency rate for short) indicates aproportion of functions that have dependencies (or call relationships)in a program. The function dependency rate may be obtained by performingthe following operations. It is assumed that a quantity of all functionsin the program is N. Subfunctions on which an entry point depends andthat are of the program are scanned from the entry point (for example,main) of the program, and all the subfunctions and the entry point forma set A. It is assumed that a quantity of functions in the set A is M.The set A and the program do not include a duplicate function (if theset A includes the duplicate function, a quantity of functions in theset A after deduplication is M). The function dependency rate of theprogram is equal to M/N.

A function coverage rate (coverage for short) indicates a proportion offunctions that have been run in a program. The function coverage ratemay be obtained by performing the following operations. It is assumedthat a quantity of all functions in the program is N; and it is assumedthat after the program runs a case set, a quantity of functions thathave been run is K. The function coverage rate is equal to K/N. Theduplicate function is counted only once in the functions that have beenrun.

A stub function is also referred to as a stub point. The stub functionis usually used to replace some interface functions and test a featureof a current function. In an implementation, the stub function in thisapplication may be a function with a small size, or may be a nullfunction. In addition, the stub function in this application may bespecially designed, so that an exception occurs when the stub functionis executed.

A computer system includes a hardware system and a software system. Thehardware system is an organic combination of various physical componentsbased on electrical, magnetic, optical, and mechanical principles, andis an entity on which the system works. The software system is variousprocedures and documents used to direct the entire system to work basedon a specified requirement. Therefore, macroscopically speaking, thesoftware system of the computer system is also a program, and may alsobe tailored. Some computer systems may provide a running environment fora third-party program, for example, a computer system on UE, a computersystem on a notebook computer, and the like.

With the rapid development of the Internet, a sales volume of computerdevices keeps increasing. For example, in 2019, the world's top 3 mobilephone manufacturers sold more than 200 million mobile phones. Faced withthe massive sales volume, other than user experience and productquality, costs of computer devices are one of important competitiveness.Generally, a computer device needs to implement a corresponding functionby using a program, and a storage medium is indispensable for storageand running of the program. In the embedding field, costs of a storagemedium are an important part of device costs. To reduce the costs of thestorage medium, miniaturization processing may be performed on aprogram. Through program miniaturization, a size of the program can bereduced. This saves storage space. There are the followingminiaturization technologies: 1. Tailoring of programs and services (forexample, Windows and Android); 2. Tailoring of software packages (forexample, Red Hat, SUSE, and Ubuntu); 3. Tailoring of files (for example,deletion of useless files through commands in Windows and Linux); and 4.Tailoring of function granularities (for example, a gc-section linkoption of a gcc compiler). The tailoring of function granularities istailoring of a minimum granularity in the foregoing tailoring, which isreferred to as function tailoring for short. The following uses the gcccompiler as an example to describe the function tailoring.

In general, the gcc compiler places each function and global variable inan independent section during source code compilation. A section can bea function or a global variable. The gcc compiler identifies adependency between each section and an entry point during a linkingprocess, and deletes a section that has no dependency with the entrypoint. The following describes this in detail. For ease ofunderstanding, some example code is introduced.

FIG. 1 is a schematic flowchart of tailoring a function based on afunction dependency rate. Operation 11 is a compilation phase. The gcccompiler compiles each function in source code 101 into an independentsection. The source code is also referred to as source code. The sourcecode 101 includes four functions: hello1( ), hello2( ), hello3( ) andmain( ). Particular content of the first three functions is not shown inFIG. 1 . It is assumed herein that there is no dependency betweenhello1( ), hello2( ), and hello3( ). The function main( ) is an entrypoint of a program. The function main( ) includes code hello1( ) andhello3( ), which means that hello1( ) and hello3( ) are called whenmain( ) is executed. In other words, there is a dependency betweenhello1( ), hello3( ), and main( ). A section set 102 can be obtainedthrough compiling by the gcc compiler. The section set 102 includes foursections that are named text.hello1 to 3 and .text.main. Operation 12 isa link preparation phase. A linker in the gcc compiler scans a callrelationship from the entry point. Based on the example in operation 1,there is a dependency between .text.hello1, .text.hello3, and.text.main. .text.hello2 is an isolated function. There is no dependencybetween text.hello2 and .text.main. Operation 13 is a linking phase. Thelinker deletes a section corresponding to the isolated function, andgenerates an executable file 104.

The foregoing function tailoring is based on the function dependencyrate. A smaller function dependency rate indicates better functiontailoring effect and a smaller size of the obtained executable file 104.The tailoring manner is usually applicable to tailoring of a programincluding an entry point (for example, main), but is not applicable totailoring of a shared library or an OS kernel (system kernel), forexample, a computer system including the shared library or the OSkernel. A running environment provided by the shared library or the OSkernel in the computer system for a third-party program may include afunction. The function has no dependency with an entry point of thecomputer system, but has a dependency with an entry point of thethird-party program. If the function is deleted by using the foregoingfunction tailoring manner, the third-party program may fail to runproperly. In view of this, a “small image-complementary image”technology is used in the industry. The technology may be divided intotwo parts. A first part is a process of obtaining the small image andthe complementary image based on source code, and a second part is aprocess of using the small image and the complementary image that aredescribed separately in the following.

The first part is usually completed by a computer device. FIG. 2 is aschematic flowchart of obtaining a small image and a complementary imagebased on source code. In operation 21, the computer device distinguishesbetween a function set B in the source code that has a dependency withan entry point, and a function set A in the source code that has nodependency with the entry point. In operation 22, the computer devicedeletes a function in the function set A from the source code, andretains a function in the function set B to obtain the small image. Inoperation 23 and operation 24, the computer device retains the functionin the function set A from the source code, and replaces the function inthe function set B with a null (null) function, that is, replaces thefunction set B with a null function set to obtain the complementaryimage. The function in the function set A in the complementary image isreferred to as a real function. The null function set in thecomplementary image corresponds to the function set B in the smallimage, or functions in the null function set in the complementary imageone-to-one correspond to functions in the function set B in the smallimage. For ease of understanding, description is provided with referenceto the source code 101 in FIG. 1 . When the source code in FIG. 2 isequal to the source code 101 in FIG. 1 , the small image is equal to theexecutable file 104, the small image includes hello1( ), hello3( ), andmain( ), and the complementary image includes hello2( ), a null function1, a null function 3, and a null function 2. The null function 1corresponds to hello1 ( ), the null function 3 corresponds to hello3( ),and the null function 2 corresponds to main( ).

The second part is the process of using the small image and thecomplementary image, and is performed by a first device on which acomputer system is installed. It is assumed that code in a third-partyprogram calls a function 1 and a function 2. The function 1 is afunction in the small image, and the function 2 is the real function inthe complementary image. When the first device starts the third-partyprogram, the first device loads the function 1 and the function 2 in aflash memory flash or a hard disk to a running memory of the firstdevice. However, in actual application, the running memory of the firstdevice is usually limited. If the code in the third-party program callsexcessive functions, the running memory may be insufficient;consequently, the third-party program fails to run.

In view of this, this application proposes a program processing method.In the method, in a process of obtaining a small image based on sourcecode, a computer device that performs an operation of a first part doesnot delete a complementary real function set, but replaces acomplementary real function with a stub function, that is, replaces thecomplementary real function set with a small stub function set. Thecomplementary real function set described herein may be the foregoingfunction set A, or may not be the function set A. When the complementaryreal function set is the function set A, the complementary real functionset is obtained based on a dependency rate, and the complementary realfunction set is a function set that is in the source code and that hasno function dependency with an entry point. When the complementary realfunction set is not the function set A, the complementary real functionset may be obtained based on a function coverage rate, and thecomplementary real function set is a function set that is in the sourcecode and that is not run in a process of running a target case set. Inaddition to replacing the complementary real function set with the smallstub function set, the computer device further retains a correspondencebetween the small stub function set and the complementary real functionset in a complementary image.

When the program execution method provided in this application is usedby the first device to run a third-party program, if the third-partyprogram includes code for calling a complementary real function, whenstarting the third-party program, the first device first loads a smallstub function corresponding to the complementary real function, insteadof directly loading the complementary real function to the runningmemory. The computer device indirectly loads the complementary realfunction by using the small stub function only when the small stubfunction is about to run or runs. The small stub function may beunderstood as a buffer for loading the complementary real function bythe computer device. Whether to load the complementary real function canbe controlled through addition of the buffer, so that a requirement ofthe third-party program for the running memory can be reduced. Forexample, when a related function corresponding to the complementary realfunction is not enabled in the third-party program, the complementaryreal function is not loaded.

For ease of understanding the technical solutions provided inembodiments of this application, a process of a processing program isfirst described. FIG. 3 is a schematic flowchart of a processing programaccording to an embodiment of this application. As shown in FIG. 3 , theprocess of the processing program may include program writing 301,program processing 302, and program execution 303. The program writing301 is a process of generating source code, including but not limited tomanual input, software generation, and the like. The program processing302 is a process of preprocessing, based on program writing, a programthat is completely or partially written, for example, automaticcorrection, tailoring, and compilation. The tailoring is deleting orreplacing some functions in the source code to reduce a size of thesource code. The program processing 302 in this embodiment of thisapplication mainly includes the foregoing program processing method. Thesource code is tailored to obtain a complementary real function set anda small real function set, and a small image and a complementary imageare obtained based on the complementary real function set and the smallreal function set. The small image includes a small real function and asmall stub function, and the complementary image includes acomplementary real function and a complementary stub function. Theprogram execution 303 is a running process of a third-party program. Thethird-party program is run to implement a related function correspondingto the third-party program. For example, when the third-party program isa program related to photographing, the related function may bephotographing, photo editing, persona beauty, or the like. In a runningprocess of the third-party program, the third-party program depends onfunctions in the small image and the complementary image to implementrelated functions. The program execution 303 in this embodiment of thisapplication mainly includes the foregoing program execution method. Afunction that the third-party program actually depends on, or a functionthat the third-party program needs to call is a complementary realfunction. The complementary real function is in the complementary image.When the third-party program needs to call the complementary realfunction, the third-party program may call the complementary realfunction by using the small stub function in the small image.

The process of the processing program is described above, and theprocess is roughly divided into the program writing 301, the programprocessing 302, and the program execution 303. The process of theprocessing program needs to be performed by a computer device. Differentoperations may be completed in different computer devices, or may becompleted in a same computer device. For example, the program writing301 is completed in a device 1, the device 1 sends the completed sourcecode to a device 2, the device 2 obtains the small image and thecomplementary image based on the source code, and the device 2 runs thethird-party program by using the small image and the complementaryimage. Alternatively, FIG. 4 is a schematic diagram of an applicationscenario according to an embodiment of this application. As shown inFIG. 4 , source code is obtained from a second device 401. The sourcecode may be compiled by the second device 401, or may be compiled byanother device. After obtaining the source code, the second device 401performs program processing 302 on the source code to obtain a smallimage and a complementary image. After obtaining the small image and thecomplementary image, the second device 401 transfers the small image andthe complementary image to a first device 402, and the first device 402performs the operation of program execution 303. In some embodiments, acomputer system in the first device 402 may perform the operation ofprogram execution 303. A program execution method in this embodiment ofthis application may be performed by the first device 402 or thecomputer system 402. In subsequent description of this embodiment ofthis application, an example in which the first device 402 is used as anexecution body is used for description. In this embodiment of thisapplication, FIG. 4 is subsequently used as an example for description.For corresponding processing in another scenario, refer to thedescription of the scenario in FIG. 4 .

In the scenario in FIG. 4 , the second device 401 may be a desktopcomputer, a notebook computer, a server, or the like. The first device402 may be a band, a washing machine, a router, a desktop computer, anotebook computer, a smartphone, or the like. The second device 401 maybe connected to the first device 402, or may not be connected to thefirst device 402. If the second device 401 is connected to the firstdevice 402, a connection mode may be a wired connection or a wirelessconnection. The wireless connection may be a wireless hotspot,Bluetooth, or the like. If the second device 401 is not connected to thefirst device 402, the small image and the complementary image may betransferred through a storage medium, for example, a USB flash drive.

The following separately describes the program processing method and theprogram execution method in embodiments of this application. FIG. 5 is aschematic flowchart of a program processing method according to anembodiment of this application.

In operation 51, a second device obtains target code based on sourcecode.

The source code may include some character strings, and the characterstrings occupy a particular size in the source code. For example,program logs are logs that need to be recorded or printed during runningof a program. For a program, logs are recorded or printed when keyinitialization or abnormal branches occur (for maintenance and testing).A log-related character string in the source code is compiled into anexecutable program to increase a size of the program. In a compiledexecutable program, a log character string usually occupies 5% to 10% ofa size of the program, and impact of size consumption on storage spaceof a first device cannot be ignored. In view of this, in thisapplication, a character string is miniaturized by using an “ID-based”method. The following provides detailed description. FIG. 6 is aschematic flowchart of miniaturizing a character string by using an“ID-based” method according to an embodiment of this application.

In operation 511, the second device obtains a character string in thesource code. For ease of understanding, the following uses source codefor description. The source code includes a FuncA function.

 Source code:  FuncA/*Function name*/  {...; /*Indicate that there maybe other code here*/  If (ret!=0) /*If ret is not equal to 0*/ {Printf(“Error:FuncA init Failed!The ret is %d\n”,ret); /*OutputError:FuncA init Failed! The ret is %d */;  }else/*Otherwise*/ {Printf(“FuncA init success!\n”);} /*Output FuncA init success!*/ Return ret;} /*Return ret */

In the foregoing source code, Error:FuncA init Failed! The ret is andFuncA init success! are two character strings. The two character stringsare a source character string 1 and a source character string 2 forshort. The source character string 1 indicates that initialization failsand a return value is output, and the source character string 2indicates that the initialization is successful. The second deviceobtains the source character string 1.

In operation 512, the second device obtains a hash value (also referredto as a hash value) of the source character string.

In operation 513, the second device determines whether a hash valueconflict occurs. In the source code, a quantity of character strings isnot determined, and only some code of a function in the source code isdescribed by using an example in operation 511. If there are a pluralityof character strings in the source code, and the second device obtainshash values of the plurality of character strings, hash values of twocharacter strings may be the same, that is, a hash value conflictoccurs. Certainly, if the source character string 1 is a first characterstring obtained by the second device, the operation in this operationmay not be performed. If the second device determines that the hashvalue conflict occurs, operation 514 is performed; otherwise, operation511 is performed.

In operation 514, a computer determines whether the source characterstring is the same as a source character string corresponding to thehash value conflict.

A target correspondence list stores a correspondence between a sourcecharacter string and a target character string. In this embodiment ofthis application, an example in which the target character string is ahash value is used for description. A hash value corresponding to thesource character string 1 is referred to as a hash value 1, and a hashvalue that conflicts with the hash value 1 is referred to as a hashvalue 3. The hash value 3 is obtained by the second device based on asource character string 3. The computer determines whether the sourcecharacter string 1 is the same as the source character string 3 in thetarget correspondence list. If the source character string 1 is the sameas the source character string 3, the source character string 1 isskipped. The second device obtains a next source character string, andrestarts from operation 511. If the source character string 1 isdifferent from the source character string 3, operation 515 isperformed.

In operation 515, the second device increases the hash value by 1. Toresolve the hash value conflict, the second device increases a hashvalue corresponding to the source character string 1 by 1.

In operation 516, the second device determines whether to resolve thehash value conflict. If no, operation 515 is performed until the hashvalue conflict is resolved. If yes, the second device obtains a nextsource character string, and restarts from operation 511.

By performing the operations in 511 to 516, the second device may obtaina target correspondence and the target character string corresponding tothe source character string. The second device replaces the sourcecharacter string in a source function with the target character stringto obtain a target function. The foregoing FuncA is the source function,and the target function is as follows:

Target function: FuncA/*Function name*/ {...; /*Indicate that there maybe other code here*/ If (ret!=0) /*If ret is not equal to 0*/{Printf(“afab9b15%d\n”, ret); /*Output afab9b15%d */ }else/*Otherwise*/{Printf(“8a52d706\n”);} /*Output 8a52d706 */ Return ret;} /*Return ret*/

In the foregoing target function, the hash value 1 is afab9b15, and ahash value corresponding to the source character string 2 is 8a52d706.The target function and the source function are the same in otherfunctions, except for different output content. The target functioncorresponds to the source function.

In operation 52, the second device obtains a small real function set anda complementary real function set based on the target code.

It can be learned from the foregoing description that the complementaryreal function set may be the function set A obtained based on thefunction dependency rate in FIG. 2 , or may be obtained based on thefunction coverage rate. In contrast, the small real function set may bethe function set B obtained based on the function dependency rate inFIG. 2 , or may be obtained based on the function coverage rate. If thecomplementary real function set is the function set A, for operation 42,refer to the foregoing process of obtaining the function set A in FIG. 2by the computer device. A process of obtaining the small real functionset and the complementary real function set based on the functioncoverage rate by the second device is described herein with emphasis. Itcan be learned from the foregoing definition of the function coveragerate that the function coverage rate is a ratio of a quantity offunctions that have been run by using source code in a process ofrunning a target case set to a quantity of functions in the source code.For ease of understanding, neither the run function nor the function inthe source code includes a duplicate function. In the process of runningthe target case set, different cases or even a same case may repeatedlycall a function. However, when a quantity of functions that have beenrun is calculated, the function is calculated only once. Based on thedefinition of the function coverage rate, definition of the small realfunction set and the complementary real function set can be obtained.The small real function set is a set of functions that are run by usingthe target code in the process of running the target case set, and thecomplementary real function set is a set of functions that are not runby using the target code in the process of running the target case set.

Before the second device runs the code, the second device compiles thetarget code, and runs the target case set by using the compiled targetcode. The target case set includes all or some functions that can beexecuted by the compiled target code. For example, the functions thatcan be executed by using the target code include three functions:photographing, photo editing, and persona beauty. The target case setincludes any one or more of the three functions. For example, the targetcase set includes photographing and photo editing. The second device maytransfer, to a device (for example, the first device), a programobtained after the target code is compiled, so that the device runsphotographing and photo editing by installing the program obtained afterthe target code is compiled, and returns function coverage informationto the second device. The second device obtains the small real functionset and the complementary real function set based on the functioncoverage information. Alternatively, the second device simulates thephotographing and photo editing functions by using the target code. Thesecond device collects statistics on a function set that has been run byusing the target code in the process of running the target case set anda function set that has not been run by using the target code in theprocess of running the target case set. The function set that has beenrun is the small real function set, and the function set that has notbeen run is the complementary real function set. A ratio of a quantityof functions in the set of run functions to a quantity of functions inthe target code is a function coverage rate of the target code. Itshould be understood that the example of the target case set herein ismerely intended to facilitate the technical solutions in embodiments ofthis application, and cannot be used as a condition for limiting thisapplication. For example, if the compiled target code may return a valueto a third-party program, returning the value to the third-party programalso belongs to a function that can be executed by the compiled targetcode.

As described above, the second device obtains the small real functionset and the complementary real function set by using the target code. Itshould be understood that, because the target code corresponds to thesource code, the second device may also obtain the small real functionset and the complementary real function set by using the source code.For example, the second device transfers, to the first device, theprogram obtained after the source code is compiled, and the first deviceperforms photographing and photo editing by installing the programobtained after the source code is compiled. The first device returns thefunction coverage information to the second device. The functioncoverage information includes a sequence number set corresponding tofunctions that have been run in the source code. In the source code andthe target code, all functions are numbered based on an arrangementsequence of the functions. In other words, each function has acorresponding sequence number. In the source code and the target code,sequence numbers of corresponding functions are the same, for example,the source function and the target function. Therefore, even if sequencenumbers returned by the first device are the sequence number setcorresponding to the functions in the source code, the second device maydetermine the small real function set in the target code based on thesequence number set, and further determine the complementary realfunction set.

In operation 53, the second device obtains a complementary stub functionset based on the small real function set, and obtains a small stubfunction set based on the complementary real function set.

The second device replaces each function in the small real function setwith a stub function to obtain the complementary stub function set, andreplaces each function in the complementary real function set with thestub function to obtain the small stub function set.

In operation 54 and operation 55, the second device obtains a smallimage based on the small real function set and the small stub functionset, and obtains a complementary image based on the complementary realfunction set and the complementary stub function set.

The second device compiles code including the small real function setand the small stub function set to obtain the small image, and thesecond device compiles code including the complementary real functionset and the complementary stub function set to obtain the complementaryimage. In other words, the second device replaces the complementary realfunction set in the source code with the small stub function set, andthen compiles the source code to obtain the small image. The seconddevice replaces the small real function set in the source code with thecomplementary stub function set, and then compiles the source code toobtain the complementary image.

The second device needs to retain a correspondence between a small stubfunction set and a complementary real function set. One small stubfunction corresponds to one complementary real function. The seconddevice may generate a mapping table, where the mapping table includes acorrespondence between a small stub function and a complementary realfunction. Alternatively, in another embodiment, the second device sets afunction name of the complementary real function to be the same as orsimilar to a function name of the small stub function to retain thecorrespondence between a small stub function and a complementary realfunction. In another embodiment, a small stub function set of a minimumimage and a complementary real function set of the complementary imageare sorted in a same order, and a small real function set of the minimumimage and a complementary stub function set of the complementary imageare sorted in a same order, so that a loader loads the function andfinds an offset address of the function. For example, the second devicemay retain the correspondence between a small stub function and acomplementary real function by using a sequence number of the function.In the small image and the complementary image, the small stub functionset and the complementary real function set number functions based onfunction arrangement orders thereof, and corresponding functions have asame sequence number. For example, the small stub function and thecomplementary real function have a same sequence number.

When running the third-party program subsequently, the first device maycall the complementary real function by using an exception of the smallstub function. To generate the exception when attempting to run thesmall stub function, the second device may set the small stub functionto be non-executable. In some embodiments, the second device summarizesall small stub functions into a separate section, aligns an address ofthe section with a page size, and sets an attribute of the section tonon-executable. When the first device attempts to run the small stubfunction, the exception occurs because the small stub function cannot beexecuted. In another embodiment, to reduce a size of the small stubfunction set, the small stub function is a null function.

Other than the foregoing method, the exception can be implemented byusing code in the small stub function. For example, the followingprovides an example of a small stub function.

 Int F4func(int a,void *b)/*function name*/  { asm volatile(  “push{r0{tilde over ( )}r2\n}” /* Push registers r0 to r2 into a stack */ “mov r0,#STUB_ABORT_ADDR\n” /*Assign a value to the r0 register, wherethe assigned value is an address at which an exception can be generatedduring access */  “mov r1,#STUB_ABORT_VALUE\n” /* Assign a value to ther1 register, where the value is immediate data */  “str r1,[r0]\n”/*Store the r1 register to the address pointed to by r0, and because r0stores the address at which the exception is generated during access,this instruction generates a data exception */  “pop r1, {r0{tilde over( )}r2 }\n”;)) /*Pop the registers r0 to r2 out of the stack*/  return0; /* Return */}

In this example, a function name of the small stub function is IntF4func. When executing the small stub function, the first device firstpushes the registers r0 to r2 into the stack, and then assigns the valueto the register r0. The assigned value is the address at which theexception can be generated during access. When “str r1,[r0]\n” isexecuted, this instruction indicates that the r1 register is stored tothe address specified by r0. Because r0 stores the address at which theexception is generated during access, this instruction generates thedata exception.

It should be noted that, in this embodiment of this application, not allmethods that can generate an exception are exhaustively listed. For aperson skilled in the art, a segment attribute/page table attribute of aCPU may be used to construct an exception, a special invalidinstruction, and a special data exception, so that the first devicegenerates the exception when attempting to run the small stub function,which all fall within the protection scope of this application.

It should be noted that operation 51 may not be performed, and in thiscase, the target code in operation 52 is the source code. If operation51 is performed, the essence of operation 51 is to replace the sourcefunction with the target function. Therefore, there is no strict timesequence requirement between operation 51 and operations 52 and 53. Forexample, after obtaining the small image and the complementary image byusing operation 55 and operation 54, the second device may replace thesource function with the target function; or the second device completesoperation 51 while performing operation 52. There is also no strict timesequence requirement between operation 54 and operation 55.

A difference between tailoring based on the function coverage rate andtailoring based on the function dependency rate is further describedherein.

First, a size of a small image obtained based on the function coveragerate is usually smaller than a size of a small image obtained based onthe function dependency rate. FIG. 7 is a schematic diagram of arelationship between a function in source code having a dependency andan overwritten function in the source code. Functions that have adependency are a set of functions have a dependency with an entry pointin source code. Overwritten functions are a set of functions that arerun in a process of running a target case set by using the source code.Generally, because a function that has a dependency with the entry pointis not necessarily run in a process of running a case set, a range ofthe function that has the dependency is usually greater than that of anoverwritten function. Therefore, the small real function set is smallerthan the function set B. Therefore, a size of a small image (small image1 for short) obtained based on the coverage rate is usually smaller thana size of a small image (small image 2 for short) obtained based on thedependency rate. Because the stub function can be a null function, asize of the small stub function is ignored herein.

When the size of the small image 1 is less than the size of the smallimage 2, a size of a complementary image 1 corresponding to the smallimage 1 is greater than a size of a complementary image 2 correspondingto the small image 2. The first device may reduce costs in some manners.For example, the first device places the small image 1 and thecomplementary image 1 in different storage media. Different storagemedia include a storage medium 1 and a storage medium 2. The small image1 is stored in the storage medium 1, and the complementary image 1 isstored in the storage medium 2. When sizes of storage space are thesame, costs of the storage medium 2 are lower than costs of the storagemedium 1. Because the size of the small image 1 is less than the size ofthe small image 2, the first device can save storage space of thestorage medium 1. In other words, costs, on the storage medium, of thefirst device are reduced. In another embodiment, the first devicelocally stores the small image 1, and the complementary image 1 isstored in a cloud platform or a central device. In this case, costs ofthe first device are also reduced. Further, the cloud platform or thecentral device provides the complementary image 1 for a plurality offirst devices, so that costs of the plurality of first devices can bereduced.

An image obtained based on the coverage rate is applicable to a scenarioof a product family. For example, in a same product family, products areclassified into low-end products (providing small-specification ortailored functions), mid-range products (providing standard functions),and high-end products (providing high-specification and rich functions).For ease of understanding, it is assumed that a function of a low-endproduct includes photographing, functions of a mid-range product includephotographing and photo editing, and functions of a high-end productinclude photographing, photo editing, and persona beauty. To reduceresearch and development costs, in modern management methods andsoftware engineering, products of a same product family usually use asame design solution and software version, that is, a same image. In thesame product family, if a software version of a high-end product isplaced in a low-end product, the low-end product may also need toprovide large storage space, which is not conducive to reducing costs ofthe low-end product. In addition, it is also a waste of storage space toput an image that includes photographing, photo editing, and personabeauty functions into the low-end product that usually requires onlyphotographing. For products of different levels, the second device mayrun the target code by using different target case sets to obtaindifferent function coverage rates, that is, obtain different smallimages and complementary images. For example, for a small image and acomplementary image that need to be used in the high-end product, thesecond device runs a target case set 1 by using the target code, wherethe target case set 1 includes photographing, photo editing, and personabeauty. For a small image and a complementary image that need to be usedin the mid-range product, the second device runs a target case set 2 byusing the target code, where the target case set 2 includesphotographing and photo editing. For a small image and a complementaryimage that need to be used in the low-end product, the second deviceruns a target case set 3 by using the target code, where the target caseset 3 includes photographing. For the source code, the functiondependency rate is fixed, but the function coverage rate can be adjustedbased on the target case set. Therefore, the image obtained based on thecoverage rate is applicable to the scenario of the product family.

In another embodiment, a product family includes low-end, mid-range, andhigh-end products. An image of the low-end product is obtained based ona target case set 1, an image of the mid-range product is obtained basedon a target case set 2, and an image of a high-end product is obtainedbased on a target case set 3. The target case set 1 is a subset of thetarget case set 2, and the target case set 2 is a subset of the targetcase set 3. Whether the three types of products belong to the sameproduct family can be determined based on source code and the imagesused by the low-end, mid-range, and high-end products, product releasetime, and product names. For example, if the source code of the threetypes of products is the same or similar, the three types of productsbelong to the same product family. For example, if the names of thethree types of products are similar, the three types of products belongto the same product family. For example, the name of the low-end productis ZXX5-m, the name of the mid-range product is ZXX5, and the name ofthe high-end product is ZXX5-pro, ZXX5-sup, or ZXX5-mate.

In another embodiment, a product family includes a low-end product and ahigh-end product. An image of the low-end product is obtained based on atarget case set 1, and an image of the high-end product is obtainedbased on a target case set 2, where the target case set 1 is a subset ofthe target case set 2.

Finally, source code (such as a shared library and an OS kernel) that“includes an external function interface” can be tailored based on thecoverage rate. When compiling the source code that “includes an externalfunction interface”, a compiler cannot predict which interfaces in anexternal interface set of the source code will be used by a third-partyprogram. For example, C library glibc provides thousands of externalfunction interfaces. When compiling C library glibc, the compiler cannotpredict which interfaces in glibc external interface set will be used bythe third-party program. Tailoring based on the coverage rate may callsome or all external function interfaces in C library glibc by setting atarget case set, so that some or all functions in the external functioninterfaces are in a small real function set. In tailoring based on thefunction dependency rate, the compiler needs to scan the dependency. Ifa program (such as the shared library) does not have an entry point, thecompiler cannot scan the function dependency from the entry point. As aresult, some or all external function interfaces in C library glibccannot be included in a set B of the function 1.

It should be noted that the small image obtained based on the coveragerate in this embodiment of this application does not necessarily includea small function. FIG. 8 is a schematic flowchart of obtaining an imagebased on a coverage rate according to an embodiment of this application.In FIG. 8 , a small real function set is a set of functions that are runby using source code in a process of running a target case set, and acomplementary real function set is a set of functions that are not runby using the source code in the process of running the target case set.A stub function set may be a null function set. For related descriptionof FIG. 8 , refer to the related description in FIG. 2 .

The foregoing describes the program processing method provided in thisapplication. According to the program processing method provided in thisapplication, the second device obtains the small image and thecomplementary image based on the source code. The small image is alsoreferred to as a first small image, and the complementary image is alsoreferred to as a first complementary image. For the small image and thecomplementary image that are obtained by using the method, the firstdevice may run the small image and the complementary image by using themethod described in FIG. 2 . In other words, the first device does notcall the complementary real function in the complementary image by usingthe small stub function in the small image. For example, refer to therelated description in FIG. 2 . The following describes a programexecution method provided in this application. In the method, a firstdevice calls a complementary real function by using a small stubfunction. For brevity of description, description related to the programexecution method in the foregoing program processing method, forexample, content included in the small image, the complementary image,and the small stub function, may be correspondingly referenced in theprogram execution method. FIG. 9 is a schematic flowchart of a programexecution method according to an embodiment of this application.

In operation 91, a first device receives a call request for a small stubfunction. When a first program is started, the first device loads, to arunning memory, a function that needs to be called by the first program.The function that needs to be called by the first program includes acomplementary real function. Therefore, the first device may receive thecall request for the small stub function (corresponding to thecomplementary real function). For example, code in the first programincludes a function name of the small stub function. The first devicepreferentially searches a small image for the function name to load thefound small stub function to the running memory. The function that needsto be called by the first program does not mean that the first devicedefinitely runs the function. When the first program does not start arelated function corresponding to the function, the function is storedin the running memory, and waits for the corresponding related functionto be started. When the corresponding related function is enabled, thefirst device runs the function. Functions that need to be called by thefirst program may include a function in a small real function set, ormay not include a function in the small real function set. The firstprogram may be in a hard disk in FIG. 9 . The first program is notnecessarily a third-party program, and may alternatively be a programbuilt in a computer system of the first device.

In operation 92, the first device loads the small stub function based onthe call request. The small image includes a small stub function set andthe small real function set, and the small stub function set includesthe small stub function. The first device loads the small stub functionin the small image based on the call request.

In operation 93, the first device determines, based on the small stubfunction, to load the complementary real function corresponding to thesmall stub function. When the first program starts the related functioncorresponding to the small stub function, the first device attempts torun the small stub function. If the detecting an exception, the firstdevice loads the complementary real function. For example, thecomplementary function is dynamically linked, and the first device callsa loader to load the complementary real function. The loader loads thecomplementary real function in a complementary image from a particularpath based on the small stub function. “A particular path” is aspecified and fixed path. For example, the system searches for a libraryin /lib and /usr/lib by default.

Two manners of generating the exception are described in the foregoingprogram processing method. One manner is to set the attributecorresponding to the section where the small stub function is located tobe non-executable. Usually, the attribute includes whether the file isreadable, writable, and executable. When the attribute is set to benon-executable, the first device cannot run the small stub function,thereby detecting the exception. Another manner is to implement theexception through content of the code in the small stub function. In thefirst manner, the first device does not run the code in the small stubfunction; and in the second manner, the first device runs a part of thecode in the small stub function.

Loading the complementary real function by detecting the exception is apreferred method in this embodiment of this application. By using themethod, the first device may know that the first program is to start therelated function corresponding to the complementary real function, andfurther load the complementary real function. In addition, before thefirst device detects the exception, the first device does not load thecomplementary real function. When the exception is detected, itindicates that running of the small stub function is not completedsuccessfully. In this case, the first program does not continue to beexecuted because execution of the small stub function is completed.Therefore, in this embodiment of this application, not only a buffer isadded for loading the complementary real function, but also the firstprogram is suspended. This provides a time basis for loading thecomplementary real function, reduces errors that may occur in the firstprogram, and improves user experience.

The foregoing describes the method for loading the complementary realfunction by detecting the exception, and the following describes amethod for loading the complementary real function without detecting theexception. The first device monitors a physical address. When a value inthe physical address is refreshed, the first device may monitor thisrefresh. The small stub function includes code for writing a value intothe physical address. When the first program runs the related functioncorresponding to the complementary real function, the first device runsthe small stub function, and writes a value into the physical address.After detecting that the value in the physical address is refreshed, thefirst device suspends running of the first program by using a processor,searches the complementary image for the complementary real functionbased on the small stub function and a correspondence, loads thecomplementary real function by using the loader, and completes the callof the first program to the complementary real function throughredirection. In the method, the first device needs to monitor a refreshstatus of the physical address at all times, and the processor needs topromptly suspend the first program. Otherwise, the first program mayconsider the small stub function as the complementary real function toskip calling the complementary real function to cause an exception in aservice of the first program.

It can be learned from the foregoing description in the programprocessing method that a second device retains the correspondencebetween a small stub function and a complementary real function. Thecorrespondence may be a sequence number or a mapping table, or may be afunction name of the small stub function and a function name of thecomplementary real function. If the correspondence is the sequencenumber, the first device may obtain a sequence number of the small stubfunction. When the complementary real function needs to be loaded byusing the small stub function, the second device loads, in thecomplementary image by using the sequence number, the complementary realfunction whose sequence number is the same as the sequence number. Ifthe correspondence is the mapping table, the first device further needsto obtain the mapping table. If the correspondence is the function name,the function name of the small stub function may be the same as orsimilar to the function name of the complementary real function.

It can be learned from the foregoing description in the programprocessing method that the second device replaces a source function insource code with a target function by using an “ID-based” method, andgenerates the target correspondence. Therefore, the small image and/orthe complementary image obtained by using the source codeinclude/includes the target function. After the first program runs thetarget function, the first device may obtain a target character string.The first device sends the target character string to the second deviceor another device, and the second device or the another device obtains asource character string by using the target correspondence and thetarget character string. If the character string is a log, a restoredsource character string is usually a language that is easy to read by aperson. It should be noted that the target function may be a function inthe small real function set, or may be a function in the complementaryreal function set. In other words, the target function may be thecomplementary real function. In another embodiment, the first device mayalso store the target correspondence. When the source character stringneeds to be read, the first device obtains the source character stringbased on the target correspondence and the target character string.

In a distributed scenario of another embodiment, the first devicelocally stores the small image 1, and the complementary image 1 isstored in the second device. The second device may be a cloud platformor a central device. The second device herein may be the second devicein the foregoing program processing method, or the second device may notbe the second device in the foregoing program processing method. Whenthe first device needs to call the complementary real function in thecomplementary image, the first device calls the complementary realfunction from the cloud platform or the central device. This reducescosts of the first device. Further, the second device provides thecomplementary image 1 for a plurality of first devices, so that costs ofthe plurality of first devices can be reduced.

In another embodiment, the small image in this embodiment of thisapplication is dynamically linked. The running memory of the firstdevice is less occupied by the first program by using the dynamic link.

In another embodiment, after calling the complementary real function byusing the program execution method in this embodiment of thisapplication, the first device sends information related to thecomplementary real function to the second device or the another device,and the second device may update the small image and the complementaryimage based on a related image. A small image and a complementary imagethat are not updated are referred to as a first small image and a firstcomplementary image, and a small image and a complementary image thatare updated are referred to as a second small image and a secondcomplementary image. Before the update, the complementary real functionis in a complementary real function set of the first complementaryimage, and after the update, the complementary real function is in asmall real function set of the second small image. In brief, the seconddevice expands a size or a range of the small real function set, andreduces a size or a range of the complementary real function set.

The foregoing describes the program processing method and the programexecution method in embodiments of this application, and the followingdescribes a program processing apparatus and a program executionapparatus in embodiments of this application. FIG. 10 is a schematicdiagram of a structure of a program execution apparatus according to anembodiment of this application.

The apparatus includes: a receiving module 1001, configured to receive acall request of a first program for a small stub function, where thesmall stub function is a function in a small stub function set, and thefirst small image includes the small stub function set and a small realfunction set;

a loading module 1002, configured to load the small stub function basedon the call request; and

a processing module 1003, configured to determine, based on the smallstub function, whether to load a complementary real functioncorresponding to the small stub function, where the complementary realfunction is a function in a complementary real function set, a firstcomplementary image includes the complementary real function set and acomplementary stub function set, the complementary stub function setcorresponds to the small real function set, and the complementary realfunction set corresponds to the small stub function set.

In another embodiment, the apparatus may further include a first sendingmodule, a second sending module, or the like. The modules in theapparatus are configured to perform all or some of the operations thatcan be performed by the first device in the embodiment corresponding toFIG. 9 .

FIG. 11 is a schematic diagram of a structure of a program processingapparatus according to an embodiment of this application.

The apparatus includes a sending module 1101, configured to send a firstsmall image to a first device. The first small image includes a smallreal function set and a small stub function set, the small stub functionset corresponds to a complementary real function set of a firstcomplementary image, the small real function set corresponds to acomplementary stub function set of the first complementary image, thesmall stub function set includes a small stub function, the small stubfunction is used by a first device to determine, based on the small stubfunction, whether to load a complementary real function corresponding tothe small stub function, and the complementary real function setincludes the complementary real function.

In another embodiment, the apparatus may further include a receivingmodule, a processing module, or the like. The modules in the apparatusare configured to perform all or some operations that can be performedby the second device in the embodiment corresponding to FIG. 5 or FIG. 6.

The foregoing describes the program processing apparatus and the programexecution apparatus in embodiments of this application, and thefollowing describes the first device and the second device inembodiments of this application.

FIG. 12 is a schematic diagram of a structure of a first device or asecond device according to an embodiment of this application.

As shown in FIG. 12 , a program processing device or a program executiondevice 1200 includes a processor 1210 and a memory 1220 coupled to theprocessor 1210. The processor 1210 may be a central processing unit(CPU), a network processor (NP), or a combination of a CPU and an NP.The processor may alternatively be an application-specific integratedcircuit (ASIC), a programmable logic device (PLD), or a combinationthereof. The PLD may be a complex programmable logic device (CPLD), afield-programmable gate array (FPGA), generic array logic (GAL), or anycombination thereof. The processor 1210 may be one processor, or mayinclude a plurality of processors.

The memory 1220 in the foregoing embodiments may include a volatilememory, for example, a random-access memory (RAM). Alternatively, thememory 1220 may include a non-volatile memory, such as a read-onlymemory (ROM), a FRAM memory, a flash memory, a hard disk drive (HDD), ora solid-state drive (SSD). Alternatively, the memory 1220 may include acombination of the foregoing types of memories.

When FIG. 12 is the schematic diagram of the structure of the programexecution device,

the processor 1210 is configured to: receive a call request of a firstprogram for a small stub function, where the small stub function is afunction in a small stub function set, and the first small imageincludes the small stub function set and a small real function set; loadthe small stub function based on the call request; and determine, basedon the small stub function, whether to load a complementary realfunction corresponding to the small stub function, where thecomplementary real function is a function in a complementary realfunction set, a first complementary image includes the complementaryreal function set and a complementary stub function set, thecomplementary stub function set corresponds to the small real functionset, and the complementary real function set corresponds to the smallstub function set.

In another embodiment, the processor 1210 is further configured to:after executing computer readable instructions in the memory 1220,perform, based on an indication of the computer readable instructions,all or some operations that can be performed by the first device, forexample, an operation performed by the first device in the embodimentcorresponding to FIG. 9 .

When FIG. 12 is the schematic diagram of the structure of the programprocessing device,

the processor 1210 is configured to send a first small image to a firstdevice. The first small image includes a small real function set and asmall stub function set, the small stub function set corresponds to acomplementary real function set of a first complementary image, thesmall real function set corresponds to a complementary stub function setof the first complementary image, the small stub function set includes asmall stub function, the small stub function is used by a first deviceto determine, based on the small stub function, whether to load acomplementary real function corresponding to the small stub function,and the complementary real function set includes the complementary realfunction.

In another embodiment, the processor 1210 is further configured to:after executing computer readable instructions in the memory 1220,perform, based on an indication of the computer readable instructions,all or some operations that can be performed by the second device, forexample, an operation performed by the second device in the embodimentcorresponding to FIG. 5 or FIG. 6 .

In the several embodiments provided in this application, it should beunderstood that the disclosed systems, apparatuses, and methods may beimplemented in other manners. For example, the described apparatusembodiments are merely examples. For example, division into units ismerely logical function division and may be other division in an actualimplementation. For example, a plurality of units or components may becombined or integrated into another system, or some features may beignored or not performed. In addition, the displayed or discussed mutualcouplings or direct couplings or communication connections may beimplemented through some interfaces. The indirect couplings orcommunication connections between the apparatuses or units may beimplemented in electronic, mechanical, or other forms.

The units described as separate parts may or may not be physicallyseparate, and parts displayed as units may or may not be physical units,may be located in one position, or may be distributed on a plurality ofnetwork units. Some or all of the units may be selected based on anactual requirement to achieve the objectives of the solutions ofembodiments.

In addition, functional units in embodiments of this application may beintegrated into one processing unit, or each of the units may existalone physically, or two or more units may be integrated into one unit.The integrated unit may be implemented in a form of hardware, or may beimplemented in a form of a software function unit.

When the integrated unit is implemented in the form of the softwarefunctional unit and is sold or used as an independent product, theintegrated unit may be stored in a computer-readable storage medium.Based on such an understanding, the technical solutions of embodimentsof this application essentially, or the part contributing to theconventional technology, all or some of the technical solutions may beimplemented in a form of a software product. The computer softwareproduct is stored in a storage medium, and includes several instructionsfor instructing a computer device (which may be a personal computer, aserver, or a network device) to perform all or some of the operations ofthe methods described in embodiments of this application. The foregoingstorage medium includes any medium that can store program code, such asa flash drive, a removable hard disk, a ROM, a RAM, a magnetic disk, oran optical disc.

1. A program execution method, applied to a computer system, the methodcomprising: receiving a call request of a first program for a small stubfunction, wherein the small stub function is a function in a small stubfunction set, and a first small image of the computer system comprisesthe small stub function set and a small real function set; loading thesmall stub function based on the call request; and determining, based onthe small stub function, whether to load a complementary real functioncorresponding to the small stub function, wherein the complementary realfunction is a function in a complementary real function set, a firstcomplementary image comprises the complementary real function set and acomplementary stub function set, the complementary stub function setcorresponds to the small real function set, and the complementary realfunction set corresponds to the small stub function set.
 2. The methodaccording to claim 1, wherein determining whether to load thecomplementary real function based on the small stub function comprises:attempting to run the small stub function according to an instruction ofthe first program, and in response to an exception being detected,loading the complementary real function.
 3. The method according toclaim 1, wherein the small real function set is a set of functions thatare run by using target code in a process of running a target case set,and the complementary real function set is a set of functions that arenot run by using the target code in the process of running the targetcase set.
 4. The method according to claim 3, wherein the method furthercomprises: loading a target function, wherein the target functioncomprises a target character string, a data volume of the targetcharacter string is less than a data volume of a source characterstring, the target code comprises the target function, the target codeis obtained based on source code, the source code comprises a sourcefunction, and the source function comprises the source character string.5. The method according to claim 4, wherein after the target functionhas been loaded, the method further comprises: sending the targetcharacter string to a second device, wherein the target character stringis used by the second device to obtain the source character string basedon a target correspondence and the target character string.
 6. Themethod according to claim 2, wherein loading the complementary realfunction comprises: loading the complementary real function from thesecond device based on the small stub function.
 7. The method accordingto claim 1, wherein the first complementary image and the first smallimage are stored in different storage media of a first device.
 8. Themethod according to claim 1, wherein the method further comprises:determining, based on the small stub function, to load the complementaryreal function, and sending related information of the complementary realfunction to the second device, wherein the related information is usedby the second device to obtain a second small image based on the relatedinformation, and the second small image comprises the small realfunction set and the complementary real function.
 9. A program executionapparatus comprising: a computer system installed in the apparatus, thecomputer system comprising a first small image, a receiver, configuredto receive a call request of a first program for a small stub function,wherein the small stub function is a function in a small stub functionset, and the first small image comprises the small stub function set anda small real function set; a loading device, configured to load thesmall stub function based on the call request; and a processing device,configured to determine, based on the small stub function, whether toload a complementary real function corresponding to the small stubfunction, wherein the complementary real function is a function in acomplementary real function set, a first complementary image comprisesthe complementary real function set and a complementary stub functionset, the complementary stub function set corresponds to the small realfunction set, and the complementary real function set corresponds to thesmall stub function set.
 10. The apparatus according to claim 9, whereinthe processing device is further configured to: attempt to run the smallstub function according to an instruction of a first program, and inresponse to the processing device detecting an exception, load thecomplementary real function.
 11. The apparatus according to claim 9,wherein the small real function set is a set of functions that are runby using target code in a process of running a target case set, and thecomplementary real function set is a set of functions that are not runby using the target code in the process of running the target case set.12. The apparatus according to claim 11, wherein the loading device isfurther configured to load a target function, wherein the targetfunction comprises a target character string, a data volume of thetarget character string is less than a data volume of a source characterstring, the target code comprises the target function, the target codeis obtained based on source code, the source code comprises a sourcefunction, and the source function comprises the source character string.13. The apparatus according to claim 12, wherein the apparatus furthercomprises: a first sending device, configured to send the targetcharacter string to a second device, wherein the target character stringis used by the second device to obtain the source character string basedon a target correspondence and the target character string.
 14. Theapparatus according to claim 10, wherein the processing device isfurther configured to load the complementary real function from thesecond device based on the small stub function.
 15. The apparatusaccording to claim 9, wherein the first complementary image and thefirst small image are stored in different storage media of the firstdevice.
 16. The apparatus according to claim 9, wherein the apparatusfurther comprises: a second sending device, configured to: determine,based on the small stub function, to load the complementary realfunction, and send related information of the complementary realfunction to the second device, wherein the related information is usedby the second device to obtain a second small image based on the relatedinformation, and the second small image comprises the small realfunction set and the complementary real function.
 17. A non-transitorycomputer readable medium storing executable instructions thereon, whichwhen executed by one or more processing devices, causes the one or moreprocessing devices to execute a program execution method, applied to acomputer system, the method comprising: receiving a call request of afirst program for a small stub function, wherein the small stub functionis a function in a small stub function set, and a first small image ofthe computer system comprises the small stub function set and a smallreal function set; loading the small stub function based on the callrequest; and determining, based on the small stub function, whether toload a complementary real function corresponding to the small stubfunction, wherein the complementary real function is a function in acomplementary real function set, a first complementary image comprisesthe complementary real function set and a complementary stub functionset, the complementary stub function set corresponds to the small realfunction set, and the complementary real function set corresponds to thesmall stub function set.
 18. The non-transitory computer readable mediumaccording to claim 17, wherein determining whether to load thecomplementary real function based on the small stub function comprises:attempting to run the small stub function according to an instruction ofthe first program, and in response to an exception being detected,loading the complementary real function.
 19. The non-transitory computerreadable medium according to claim 17, wherein the small real functionset is a set of functions that are run by using target code in a processof running a target case set, and the complementary real function set isa set of functions that are not run by using the target code in theprocess of running the target case set.
 20. The non-transitory computerreadable medium according to claim 19, wherein the method furthercomprises: loading a target function, wherein the target functioncomprises a target character string, a data volume of the targetcharacter string is less than a data volume of a source characterstring, the target code comprises the target function, the target codeis obtained based on source code, the source code comprises a sourcefunction, and the source function comprises the source character string.